package com.edu.modules.baseinfo.service;

import com.alibaba.fastjson.JSONObject;
import com.edu.modules.baseinfo.dao.AccountDao;
import com.edu.modules.baseinfo.dao.StudentDao;
import com.edu.modules.baseinfo.dao.TeacherDao;
import com.edu.modules.baseinfo.vo.ResultVo;
import com.edu.modules.comm.Utils;
import com.edu.modules.entity.TAccountEntity;
import com.edu.modules.entity.TTeacherEntity;
import com.sari.common.service.BaseService;
import com.sari.common.utils.DateUtils;
import com.sari.common.utils.IdGen;
import com.sari.common.utils.Reflections;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by kwj on 2016/7/15.
 */
@Service
public class TeacherService extends BaseService {

	@Autowired
	private TeacherDao teacherDao;

    @Autowired
    private StudentDao studentDao;

    @Autowired
    private AccountDao accountDao;

	public ResultVo saveTeacher(JSONObject param){

		String msg = null;
		boolean flag = false;

		TTeacherEntity Teacher = new TTeacherEntity();
		String teacherId = IdGen.uuid();
        String time = DateUtils.getDate();
		param.put("teacherId", teacherId);
		param.put("createTime", DateUtils.getDate());
		Reflections.initBean(Teacher,param);
        try {
			teacherDao.saveObject(Teacher);

            TAccountEntity account = new TAccountEntity();
            account.setId(IdGen.uuid());
            account.setUserName(param.getString("teacherPhone"));
            account.setPassWord("666666");
            account.setUserRole("2");
            account.setUserId(teacherId);
            account.setCreateTime(time);
            account.setUpdateTime(time);
            accountDao.saveObject(account);

			msg = "success";
			flag = true;
		}catch (Exception e){
			msg = "fail";
			flag = true;
		}
		return new ResultVo(flag,msg);
	}

    public JSONObject getTeacherByPage(JSONObject param) {

        int offset = param.getInteger("offset");
        int limit = param.getInteger("limit");
        String teacherName = param.getString("teacherName");
        String teacherPhone = param.getString("teacherPhone");
        String subjectType = param.getString("subjectType");

        List list = new ArrayList();
        list = teacherDao.getTeacherByPage(teacherName,teacherPhone,subjectType,offset,limit);

        JSONObject result= new JSONObject();
        result.put("total",teacherDao.getTeacherCounts(teacherName,teacherPhone,subjectType));
        result.put("rows", list);

        return result;
    }

    public JSONObject  getTeacherLessonStatistics(JSONObject param){

	    int offset = param.getInteger("offset");
        int limit = param.getInteger("limit");
        String teacherName = param.getString("teacherName");
        String time = param.getString("time");

        List list = new ArrayList();
        list = teacherDao.getTeacherLessonStatistics(teacherName,time,offset,limit);

        JSONObject result= new JSONObject();
        result.put("total",teacherDao.getTeacherLessonStatisticsCount(teacherName,time));
        result.put("rows", list);

        return result;
    }

    public JSONObject  getTeacherClassStatistics(JSONObject param){

        String teacherName = param.getString("teacherName");
        String grade = param.getString("grade");

        List countsList = new ArrayList();
        List<Map> dateList = Utils.getDateList();
        for(Map m : dateList){

            countsList.add(teacherDao.getTeachersStudentCount(teacherName,grade,
                    m.get("startDate").toString(),m.get("endDate").toString()));
        }

        JSONObject result= new JSONObject();
        result.put("studentCounts",studentDao.getAllStudentCount());
        result.put("countsList",countsList);
        result.put("rows", teacherDao.getStudentCountsByTeacher(teacherName,grade));

        return result;
    }

    public JSONObject getTeacherByLesson(String lesson) {

        JSONObject result= new JSONObject();
        result.put("teachers",teacherDao.getTeacherByLesson(lesson));

        return result;
    }

    public void updateTeacher(JSONObject param){

        TTeacherEntity objInfo = new TTeacherEntity();
		param.put("createTime", DateUtils.getDate());
        Reflections.initBean(objInfo,param);
        teacherDao.updateObject(objInfo);
    }

    public ResultVo deleteTeacher(String teacherId){

        teacherDao.deleteObjects("t_teacher","teacher_id",teacherId);
        return new ResultVo(true,"success");
    }

    public String getTeacherName() {

        String groupStr = "";
        List<Map> list = teacherDao.getTeacherName();
        for (Map m : list){
            String name = m.get("teacher_name").toString();
            String subject = m.get("subject_type").toString();
            groupStr += "<option value=\"" + name +"\">"+name+"&nbsp;&nbsp;&nbsp;&nbsp;("+subject+"教师)"+"</option>";
        }
        return groupStr;
    }

    public String getTeachers(){

        String groupStr = "";
        List<Map> list = teacherDao.getTeacherName();
        for (Map m : list){
            String name = m.get("teacher_name").toString();
            groupStr += "<option value=\"" + name +"\">"+name+"</option>";
        }
        return groupStr;
    }

    public List getTeachersLesson(){

        List resultList = new ArrayList();
        List<Map> list = teacherDao.getTeachersLesson();
        for (Map m : list){
            resultList.add(m.get("subject_type").toString());
        }

        return resultList;
    }

    public JSONObject  getTeacherEvaluateByPage(JSONObject param){

        int offset = param.getInteger("offset");
        int limit = param.getInteger("limit");
        String lessonName = param.getString("lessonName");
        String startTime = param.getString("startTime");
        String endTime = param.getString("endTime");

        List list = new ArrayList();
        list = teacherDao.getTeacherEvaluateByPage(lessonName,startTime,endTime,offset,limit);

        JSONObject result= new JSONObject();
        result.put("total",teacherDao.getTeacherEvaluateCount(lessonName,startTime,endTime));
        result.put("rows", list);

        return result;
    }

    public ResultVo deleteEvaluate(String evaluateId){

        teacherDao.deleteObjects("t_lesson_evaluation","evaluate_id","'"+evaluateId+"'");

        return new ResultVo(true,"success");
    }
}
